<!--
 * @Description: 需求计划
 * @Author: wangwangwang
 * @Date: 2020-09-21 14:09:06
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2021-12-29 15:01:58
-->
<template>
  <div class="pages_content table">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main>
        <div>
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px">特材成品名称</span>
                  <el-input v-model="tableParam.productName" style="width: 160px" placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">成品型号</span>
                  <el-input v-model="tableParam.productType" style="width: 140px" placeholder="请输入" @keyup.enter.native="gettableData()" />
                </span>
                <span style="margin-left: 24px">
                  <span style="margin-right: 12px">需求站点</span>
                  <el-cascader ref="stationIdList" v-model="stationIdList" :options="siteIdListTree" :show-all-levels="false" placeholder="请选择" @change="tableParam.stationId  = $refs.stationIdList.getCheckedNodes()[0].value"></el-cascader>
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
                <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="statisticalDialog=true">交易统计</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card" style="padding-top:10px;width:100%;">
            <div v-loading="tableLoading" class="card_info">
              <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe :data="tableData" tooltip-effect="dark">
                <template v-for="(item, index2) in table">
                  <el-table-column v-if="item.status" :key="index2" :label="item.label" align="center" :width="item.width">
                    <template slot-scope="scope">
                      <span v-if="item.prop==='sum'" style="cursor: pointer;" @click="showTransport(scope.row)">
                        {{scope.row.sum}}
                      </span>
                      <span v-else-if="item.label == '状态'">
                        <span v-if="scope.row.dataStatus==1">待处理</span>
                        <span v-else-if="scope.row.dataStatus==2">已接单</span>
                      </span>
                      <span v-else-if="item.label == '操作'">
                        <el-button v-if="scope.row.dataStatus==1 && ButtonList.findIndex(res=>(res.buttonValue=='JIEDAN'))!==-1" class="bottom_one" size="mini" @click="look1Fun('接单',scope.row)">
                          接单
                        </el-button>
                        <el-button v-if="scope.row.dataStatus==2 && ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" size="mini" @click="look1Fun('修改',scope.row)">
                          修改
                        </el-button>
                      </span>
                      <span v-else>{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
          </div>
        </div>
      </el-main>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total }}条</div>
          <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" :page-sizes="[10, 20, 30, 40,50,100,200,500,1000]" layout="prev,pager,sizes,next,jumper" :total="total" @size-change="sizeChange" @current-change="gettableData()"></el-pagination>
        </div>
        <div>
          <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="gettableData">刷新</el-button>
        </div>
      </div>
    </el-container>
    <!-- 接单/修改 -->
    <el-dialog v-dialogDrag :visible.sync="look1Type" width="900px">
      <span slot="title" class="dialog-title">{{dataStatus}}</span>
      <div class="dialoginfo">
        <el-form ref="formData1" :inline="true" :model="formData1" label-width="130px">
          <el-form-item prop="planTime" :rules="[{required: true,message: '请选择预计交货时间',trigger: 'blur',}]">
            <span slot="label" style="color: #ff0000">预计交货时间</span>
            <el-date-picker v-model="formData1.planTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择"></el-date-picker>
          </el-form-item>
        </el-form>
        <div>
          <div style="padding-bottom: 10px;">
            <el-button size="mini" class="bottom_all" style="margin-left: 0;" @click="addItem()">
              <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
              添加
            </el-button>
          </div>
          <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="arrList1" tooltip-effect="dark" :row-class-name="tableRowClassName">
            <el-table-column label="交货司机" align="center">
              <template slot-scope="scope">
                <el-select v-model="scope.row.driverId" placeholder="请选择" style="width: 100%;" @change="scope.row.driver=driverIdList[driverIdList.findIndex((res)=>res.id==scope.row.driverId)].name">
                  <el-option v-for="(item, index0) in driverIdList" :key="index0" :label="item.name" :value="item.id"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="车牌号" align="center">
              <template slot-scope="scope">
                <el-select v-model="scope.row.carNo" placeholder="请选择" style="width: 100%;">
                  <el-option v-for="(item, index2) in carNoIdList" :key="index2" :label="item.carNo" :value="item.carNo"></el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="运输量" align="center">
              <template slot-scope="scope">
                <el-input v-model="scope.row.traffic"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="left" prop="specification">
              <template slot-scope="scope">
                <el-button v-if="scope.$index!=0" size="mini" class="bottom_two" @click="deleteItem(scope.$index)">
                  <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="look1Type =false ">取 消</el-button>
        <el-button class="bottom_all" style="margin-left: 24px;" @click="submit1()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 交易统计 -->
    <el-dialog v-dialogDrag :visible.sync="statisticalDialog" width="880px">
      <span slot="title" class="dialog-title">交易统计</span>
      <div>
        <div class="search">
          <span style="display: inline-block;margin-bottom: 10px;">
            <span style="margin-right: 12px">公司</span>
            <el-select v-model="dialogFormDate.company" placeholder="请选择">
              <el-option v-for="(item, index) in companyList" :key="index" :label="item" :value="item"></el-option>
            </el-select>
          </span>
          <span style="margin-left: 24px">
            <span style="margin-right: 12px">站点</span>
            <el-select v-model="dialogFormDate.site" placeholder="请选择">
              <el-option v-for="(item, index) in siteList" :key="index" :label="item" :value="item"></el-option>
            </el-select>
          </span>
          <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), gettableData()">查 询</el-button>
          <el-button size="medium" class="bottom_all_two" style="margin-left: 12px" @click="init()">重 置</el-button>
          <div>
            <span style="margin-right: 12px">查询时间段</span>
            <el-date-picker v-model="dialogTime" style="height:36px" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </div>
        <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width: 100%" :data="dialogTableData" tooltip-effect="dark">
          <el-table-column label="特材成品"></el-table-column>
          <el-table-column label="需求总量"></el-table-column>
          <el-table-column label="收货总量"></el-table-column>
          <el-table-column label="返料总量"></el-table-column>
        </el-table>
        <div class="dialogTotal">合计：xxxxxxx 吨</div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="statisticalDialog =false ">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog v-dialogDrag :visible.sync="tansprotVisible" width="90%">
      <span slot="title" class="dialog-title">运输任务</span>
      <el-table :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tansportList" tooltip-effect="dark">
        <el-table-column label="运输编号" prop="code"></el-table-column>
        <el-table-column label="站点" prop="station"></el-table-column>
        <el-table-column label="特材成品名称" prop="productName"></el-table-column>
        <el-table-column label="成品型号" prop="productType"></el-table-column>
        <el-table-column label="出货量" prop="shipment"></el-table-column>
        <el-table-column label="第一次过磅量" prop="firstWeighing"></el-table-column>
        <el-table-column label="第二次过磅量" prop="secondWeighing"></el-table-column>
        <el-table-column label="收货量" prop="recNum"></el-table-column>
        <el-table-column label="入库量" prop="receipt"></el-table-column>
        <el-table-column label="司机" prop="driver"></el-table-column>
        <el-table-column label="车牌号" prop="carNo"></el-table-column>
        <el-table-column label="状态" prop="dataStatus">
          <template slot-scope="scope">
            <span>{{['待出货', '待收货', '已收货', '已入库'][scope.row.dataStatus]}}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="card_bottom">
        <div class="card_bottom_left">
          <div class="top_page_totle">总共{{ total2 }}条</div>
          <el-pagination :current-page.sync="page2" layout="prev,pager,next" :total="total2" @current-change="changePage()"></el-pagination>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="tansprotVisible =false ">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/ICPSmixin.js";
import {
  queryPage, needupdateData, carqueryPage, shippingqueryPage, queryShippingPage
} from "@/api/smart/specialmaterial/Deplan.js";
import { getUserAutList, get_simple_user_listBycompanyId } from "@/api/common.js";
export default {
  mixins: [mixin],
  data() {
    return {
      stationIdList: [],
      siteIdListTree: [],
      /*表格增删改查及下载接口*/
      queryPage: queryPage,
      /*表格数据*/
      table: [
        {
          label: "ID",
          width: "130",
          prop: "id",
          status: true,
        },
        { label: "状态", width: "100", prop: "dataStatus", status: true },
        {
          label: "接收时间",
          width: "140",
          prop: "createTime",
          status: true,
        },
        { label: "特材成品名称", width: "130", prop: "productName", status: true },
        { label: "成品型号", width: "120", prop: "productType", status: true },
        { label: "需求量", width: "120", prop: "needNum", status: true },
        { label: "已交货量", width: "120", prop: "sum", status: true },
        { label: "需求时间", width: "140", prop: "needTime", status: true },
        { label: "需求站点", width: "120", prop: "station", status: true },
        { label: "备注", width: "auto", prop: "remark", status: true },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      look1Type: false,
      arrList1: [],
      formData1: { planTime: '' },
      dataStatus: '',
      driverIdList: [],
      carNoIdList: [],
      /**交易统计*/
      statisticalDialog: false,
      dialogFormDate: { company: '', site: '' },
      companyList: [],
      siteList: [],
      dialogTime: null,
      dialogTableData: [],
      tansportList: [],
      page2: 1,
      total2: 0,
      tansprotVisible: false,
      planId: '',
    };
  },
  async created() {
    this.siteIdListTree = await getUserAutList()
    this.gettableData();
    carqueryPage({ pageSize: 99999, page: 1 }).then((res) => {
      this.carNoIdList = res.data.data
    })
  },
  methods: {
    submit1() {
      this.$refs.formData1.validate((valid) => {
        if (valid) {
          let type = false
          this.arrList1.forEach((v) => {
            if (v.driverId == '' || v.carNo == '') {
              type = true
            }
          })
          if ([...new Set(this.arrList1.map((item) => item.driverId))].length < this.arrList1.length) {
            this.$message({
              message: '交货司机不能重复',
              type: 'warning'
            });
            return
          }
          if (type) {
            this.$message({
              message: '表格信息未填写完整！',
              type: 'warning'
            });
            return
          }
          this.formData1.list = this.arrList1
          console.log(JSON.parse(JSON.stringify(this.formData1)))
          needupdateData(this.formData1).then((res) => {
            if (res.code == 1001) {
              this.$message({
                message: '保存成功',
                type: 'success'
              });
              this.look1Type = false
              this.gettableData();
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addItem() {
      this.arrList1.push({
        driver: "",//司机
        driverId: "",//司机id
        carNo: "",//车牌号
      })
    },
    deleteItem(index) {
      if (this.arrList1[index].id) {
        this.arrList1[index].status = 0
      } else {
        this.arrList1.splice(index, 1)
      }
    },
    async look1Fun(name, row) {
      this.look1Type = true
      this.dataStatus = name
      this.arrList1 = []
      /**查询特材库司机用户 */
      await get_simple_user_listBycompanyId({
        roleId: "87b62423cc0c4a1c9a775a08d9bf29e4",
        orgznizationId: row.companyId
      }).then((res) => {
        this.driverIdList = res.data;
      });
      if (name == '接单') {
        this.arrList1.push({
          driver: "",//司机
          driverId: "",//司机id
          carNo: "",//车牌号
        })
        console.log(JSON.parse(JSON.stringify(this.arrList1)))
      } else {
        shippingqueryPage({ needId: row.id, pageSize: 99999, page: 1, dataType: 1 }).then((res) => {
          this.arrList1 = res.data.data
        })
      }
      this.formData1 = { ...this.formData1, ...row }
      this.$refs.formData1.resetFields();
    },
    showTransport (data) {
      this.planId = data.id;
      this.tansprotVisible = true;
      this.initTransPortList();
    },
    changePage () {
      this.initTransPortList();
    },
    initTransPortList () {
      const params = {
        id: this.planId,
        statusList: [2, 3],
        page: this.page2,
        pageSize: 10,
      }
      queryShippingPage(params).then(res => {
        if (res.code === 1001) {
          this.tansportList = res.data.data;
          this.total2 = res.data.totalRows;
        }
      })
    },
    /*重置数据*/
    init() {
      this.stationIdList = [];
      this.tableParam.productName = "";
      this.tableParam.productType = "";
      this.tableParam.stationId = "";
      this.gettableData();
    },
    tableRowClassName({ row }) {
      if (row.status == 0) {
        return 'display-row-Fzy';
      }
      return '';
    },
    sizeChange(value) {
      this.tableParam.pageSize = value;
      this.gettableData();
    },
  },
};
</script>
<style>
.display-row-Fzy {
  display: none;
}
.dialogTotal {
  line-height: 40px;
  text-align: center;
  border: 1px solid #e1e5ed;
  border-top: 0;
}
</style>
